package com.example.tool.activtiy;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.CardView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.util.DebugLog;

import butterknife.BindView;

import static com.example.tool.util.DebugLog.ANIMATION;

/**
 * Created Date 2019/8/30
 *
 * @author liangkun
 * description：属性动画
 * 两个使用方法类：ValueAnimator 类 & ObjectAnimator 类
 * 两个辅助使用类：插值器 & 估值器
 */
public class PropertyAnimActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.card_home_item)
    protected CardView cardOne;
    @BindView(R.id.card_home_two)
    protected CardView cardTwo;
    @BindView(R.id.card_home_three)
    protected CardView card_home_three;
    @BindView(R.id.tv_three)
    protected TextView tv_three;

    @BindView(R.id.iv_anim)
    protected ImageView ivAnim;
    @BindView(R.id.pg)
    protected ProgressBar pg;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    private ValueAnimator btnAnimator;


    @Override
    protected int getLayoutId() {
        return R.layout.property_anim;
    }

    @Override
    protected void initView() {
        tvTitle.setText("属性动画");
        // 创建动画作用对象：此处以Button为例

// 步骤1：设置属性数值的初始值 & 结束值
        btnAnimator = ValueAnimator.ofInt(cardOne.getLayoutParams().width, cardOne.getLayoutParams().width + 100);
        // 初始值 = 当前按钮的宽度，此处在xml文件中设置为150
        // 结束值 = 500
        // ValueAnimator.ofInt()内置了整型估值器,直接采用默认的.不需要设置
        // 即默认设置了如何从初始值150 过渡到 结束值500

// 步骤2：设置动画的播放各种属性
        btnAnimator.setDuration(2000);
        // 设置动画运行时长:1s


    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
        // 步骤3：将属性数值手动赋值给对象的属性:此处是将 值 赋给 按钮的宽度
        // 设置更新监听器：即数值每次变化更新都会调用该方法
        cardOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnAnimator.start();
                btnAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        int currentValue = (Integer) animator.getAnimatedValue();
                        // 获得每次变化后的属性值
                        System.out.println(currentValue);
                        // 输出每次变化后的属性值进行查看
                        cardOne.getLayoutParams().width = currentValue;
                        // 每次值变化时，将值手动赋值给对象的属性
                        // 即将每次变化后的值 赋 给按钮的宽度，这样就实现了按钮宽度属性的动态变化
                        // 步骤4：刷新视图，即重新绘制，从而实现动画效果
                        cardOne.requestLayout();

                    }
                });


                ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100, 50, 80, 100); //0到100，到50，
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
//                        DebugLog.i(ANIMATION + " onAnimationUpdate: " + animation.getAnimatedValue());
                        pg.setProgress((Integer) animation.getAnimatedValue());
                    }
                });
                valueAnimator.setInterpolator(new LinearInterpolator());
                valueAnimator.setRepeatCount(1);
                valueAnimator.setDuration(3000);
                valueAnimator.start();
                //监听
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        DebugLog.i(ANIMATION + " onAnimationStart: ");
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        DebugLog.i(ANIMATION + "onAnimationEnd");
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        DebugLog.i(ANIMATION + "onAnimationCancel");
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                        DebugLog.i(ANIMATION + "onAnimationRepeat " + animation.getDuration());
                    }
                });

//                ValueAnimator valueAnimatorRgb = ValueAnimator.ofArgb(0xffffffff,0xffff0000,0xff0000ff); //0到100，到50，
//                valueAnimatorRgb.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                    @Override
//                    public void onAnimationUpdate(ValueAnimator animation) {
//
//                        cardOne.setBackgroundColor((Integer) animation.getAnimatedValue());
//                    }
//                });
//                valueAnimatorRgb.setDuration(3000);
//                valueAnimatorRgb.start();

            }
        });
        cardTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator rotation = ObjectAnimator.ofFloat(ivAnim,"rotation",0,270,50);
                ObjectAnimator translationX = ObjectAnimator.ofFloat(ivAnim,"translationX",0,300);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(ivAnim,"alpha",0.1f,1f);
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivAnim,"scaleX",1f,1.2f);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playSequentially(rotation,translationX,alpha,scaleX);
                animatorSet.setDuration(5000);
                animatorSet.setInterpolator(new BounceInterpolator());
                animatorSet.start();
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
            }
        });


          card_home_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofInt(tv_three,"textColor",0xffffffff,0xffff0000);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(Animation.INFINITE);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });




    }


    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

}
